//
//  SettingTableViewController.m
//  tingche
//
//  Created by 尤振宇 on 2017/6/16.
//  Copyright © 2017年 尤振宇. All rights reserved.
//

#import "SettingTableViewController.h"
#import "Config.h"
#import "JHMacros.h"
#import "Config.h"
#import <SDImageCache.h>
#import <EaseUI.h>
#import "WebViewController.h"

static NSInteger   cellNumber = 6;
static NSInteger   headerNumber = 3;

static CGFloat     cellHeight = 44;
static CGFloat     headerHeight = 5;

@interface SettingTableViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginOutBtn;
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;
@property (weak, nonatomic) IBOutlet UISwitch *pushSwitch;

@end

@implementation SettingTableViewController

-(instancetype)init
{
    return [[UIStoryboard storyboardWithName:@"Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"SettingTableViewController"];
}

- (IBAction)switchAction:(UISwitch *)sender {
    
    [Config userDefaultsSetObject:[NSString stringWithFormat:@"%d",sender.on] key:kIsOpenRemotePush];
    if (sender.on) {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }else{
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    }
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.loginOutBtn borderWidthAndColor:k65ACFE];
    [self.loginOutBtn roundedCorners:3];
    
    [Tool folderSizeAtPath:kAppCachesPath operation:^(float folderSize) {
        self.cacheLabel.text = [Tool fileSize:folderSize + [[SDImageCache sharedImageCache] getSize] / 1024 /1024];
    }];
    
    NSString *isOpen = [Config userDefaultsGetObjectForKey:kIsOpenRemotePush];
    if (isOpen) {
        BOOL on = [isOpen boolValue];
        self.pushSwitch.on = on;
    }else{
        self.pushSwitch.on = YES;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = kF2F2F2;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.001;
    }else{
        return headerHeight;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        CGFloat cellH = tableView.height - (cellNumber * cellHeight) - (headerNumber * headerHeight) - 64;
        return  cellH < 45 ? 45 : cellH;
    }else{
        return cellHeight;
    }
}



- (IBAction)logOutAction:(id)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kLogOutNotification object:nil];
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"PushChangeMobile"] || [identifier isEqualToString:@"PushChangePassword"]) {
        if ([Config isLogin]) {
            return YES;
        }else{
//            [WKHUDView setFail:@"请先登录"];
            return NO;
        }
    }else if ([identifier isEqualToString:@"ClearCache"]){
     
        [MBProgressHUD showMessage:@"正在清理..."];
        
        [Tool clearFolder:kAppCachesPath mainThread:^{
            [Tool folderSizeAtPath:kAppCachesPath operation:^(float folderSize) {
                self.cacheLabel.text = [Tool fileSize:folderSize + [[SDImageCache sharedImageCache] getSize] / 1024 /1024];
                [MBProgressHUD hideHUD];
            }];
        }];
        
        return NO;
    }else{
        return YES;
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"withdrawIdentifier"]) {
        WebViewController *vc = [segue destinationViewController];
        vc.webType = WebTypeCashExplain;
        vc.title = @"提现说明";
    }else if ([segue.identifier isEqualToString:@"userAgreementIdentifier"]){
        WebViewController *vc = [segue destinationViewController];
        vc.webType = WebTypeUserAgreement;
        vc.title = @"用户协议";
    }
}

-(void)dealloc
{
    NSLog(@"SettingTableViewController dealloc");
}


@end
